package bid.yanhj.yanhaijiang.viewpager;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * ViewPager使用
 *
 * 1 插入 ViewPager标签
 * 初始化
 * mainTv = this.<ViewPager>findViewById(R.id.main_tv);
 *
 * 2 ViewPager 数据加载
 *  List<View> views = new ArrayList<>();
 *  View viewText = LayoutInflater.from(this).inflate(R.layout.view_text, null);
 *  views.add(viewText);
 *
 * 3 新建类继承 PagerAdapter
 *
 * 4 编写适配器方法
 * 5 初始化适配器 并绑定ViewPager
 *
 * 6 设置默认显示界面 监听事件
 */
public class MainActivity extends AppCompatActivity {

    private ViewPager mainTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainTv = this.<ViewPager>findViewById(R.id.main_tv);

        // 组装view
        List<View> views = new ArrayList<>();
        View viewText = LayoutInflater.from(this).inflate(R.layout.view_text, null);
        views.add(viewText);

        View viewImage = LayoutInflater.from(this).inflate(R.layout.view_img, null);
        views.add(viewImage);

        View viewColor = LayoutInflater.from(this).inflate(R.layout.view_color, null);
        views.add(viewColor);


        // 适配器和ViewPager建立联系 显示屏幕
        MainVpAdpter mainVpAdpter = new MainVpAdpter(views);
        mainTv.setAdapter(mainVpAdpter);

        // 设置默认显示界面 监听事件
        mainTv.setCurrentItem(1);

        mainTv.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            /**
             * 选中某页
             * @param position
             */
            @Override
            public void onPageSelected(int position) {

                Log.d("yanhj", String.valueOf(position));
            }

            /**
             * 滚动
             * @param state
             */
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
